package com.slipper.system.codeless.controller;

import com.slipper.constants.Constants;
import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.bo.MobilePageBO;
import com.slipper.system.codeless.service.MobilePageService;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;

import java.util.List;

@RestController
@RequestMapping("/system/mobilePage")
public class MobilePageController {
    @Resource
    private MobilePageService mobilePageService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody MobilePageBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(mobilePageService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody MobilePageBO bo) {
        return ResultUtil.successResult(mobilePageService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody MobilePageBO bo) {
        return ResultUtil.successResult(mobilePageService.queryPage(bo));
    }

    @PostMapping(value = "/queryTree")
    public String queryTree(@RequestBody MobilePageBO bo) {
        List<MobilePageBO> folderList = mobilePageService.queryList(bo);
        TreeInfo treeInfo = new TreeInfo("页面树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setTypeField("type");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, folderList, "asc"));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody MobilePageBO bo) {
        return ResultUtil.saveResult(mobilePageService.save(bo), bo);
    }

    @PostMapping(value = "/saveJsonList")
    public String saveJsonList(@RequestBody List<MobilePageBO> pageList) {
        return ResultUtil.saveResult(mobilePageService.saveJsonList(pageList));
    }

    @PostMapping(value = "/copy")
    public String copy(@RequestBody MobilePageBO bo) {
        if (mobilePageService.copy(bo) > 0) {
            return ResultUtil.successResult("复制成功！");
        } else {
            return ResultUtil.failureResult("复制失败！");
        }
    }

    @PostMapping(value = "/updateName")
    public String updateName(@RequestBody MobilePageBO bo) {
        return ResultUtil.operateResult(mobilePageService.updateName(bo), "修改名称");
    }

    @PostMapping(value = "/updatePosition")
    public String updatePosition(@RequestBody MobilePageBO bo) {
        return ResultUtil.saveResult(mobilePageService.updatePosition(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody MobilePageBO bo) {
        return ResultUtil.deleteResult(mobilePageService.delete(bo));
    }
}
